﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.Data.Odbc;
using System.Collections.Specialized;

namespace EuroAudit
{
    
    public partial class Main : System.Web.UI.Page
    {
        GlobalF GFunc = new GlobalF();

        protected void Page_Load(object sender, EventArgs e)
        {

            string[] VID = null;
            string[] NP = null;
            string[] VPRM = null;
            string[] NText = null;
            string[] UserEditBranch;

            DataSet ds;
            string QString;
            string qsRep;
            string qsDoc;
            string qsIn;
            string qsOut;

            if (Session["UserName"] == null)
            {
                Response.Write("Sesija ir pārtraukta.");
                Response.Redirect("Login.aspx");
            }
            //Table1.Rows[0].Cells[0].Text = "SIA TRIĀLS";
            TableRow NewRow = new TableRow();
            TableCell NewCell = new TableCell();
            NewCell.Text = "SIA EUROAUDIT";
            NewCell.Font.Bold = true;
            NewCell.Font.Size = 18;
            NewCell.VerticalAlign = VerticalAlign.Middle;
            NewCell.HorizontalAlign = HorizontalAlign.Center;
            NewRow.Cells.Add(NewCell);
            Table1.Rows.Add(NewRow);
            NewRow = new TableRow();
            NewCell = new TableCell();
            NewCell.Text = "ARHĪVS";
            NewCell.Font.Bold = true;
            NewCell.Font.Size = 18;
            NewCell.VerticalAlign = VerticalAlign.Middle;
            NewCell.HorizontalAlign = HorizontalAlign.Center;
            NewRow.Cells.Add(NewCell);
            Table1.Rows.Add(NewRow);
            


            NameValueCollection HRStr = Request.QueryString;
            if (HRStr.GetValues("ViewID") != null)
            {
                VID = HRStr.GetValues("ViewID");
                VPRM = HRStr.GetValues("ViewParam");
                NP = HRStr.GetValues("NodePath");
                NText = HRStr.GetValues("NodeText");
                UserEditBranch = HRStr.GetValues("UserEditBranch");
                switch (Convert.ToInt32(VID[0]))
                {
                    case 0://jaunumi
                        TableRow NRow;
                        TableCell NCell;
                        HyperLink NHLink;
                        if (Session["UserName"] == null || Session["UserName"].ToString() == "" || Session["UserName"].ToString() == "guest")
                        {
                            //Response.Write("Guest content.");

                            qsRep = "SELECT reports.RepID, reports.RepName, reports.RepCaption, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM reports LEFT JOIN tcontent ON (reports.RepGroup=tcontent.ViewParam) WHERE reports.Laiks>=CurDate()-7 AND tcontent.ViewID=3 AND (reports.AccessU LIKE '%;guest;%' OR reports.AccessG LIKE '%;Guests;%') ORDER BY reports.Laiks DESC";
                            qsDoc = "SELECT docs.DocID, docs.DocHead, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM docs LEFT JOIN tcontent ON (docs.DocType=tcontent.ViewParam) WHERE docs.Laiks>=CurDate()-7 AND tcontent.ViewID=2 AND (docs.AccessU LIKE '%;guest;%' OR docs.AccessG LIKE '%;Guests;%') ORDER BY docs.Laiks DESC";
                            //dsLogin.Select(DataSourceSelectArguments.Empty);
                        }
                        else
                        {
                            //Response.Write(Session["UserName"] + " content.");
                            if (Session["IsAdmin"].ToString() == "1")
                            {
                                qsRep = "SELECT reports.RepID, reports.RepName, reports.RepCaption, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM reports LEFT JOIN tcontent ON (reports.RepGroup=tcontent.ViewParam) WHERE reports.Laiks>=CurDate()-7 AND tcontent.ViewID=3 ORDER BY reports.Laiks DESC";
                                qsDoc = "SELECT docs.DocID, docs.DocHead, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM docs LEFT JOIN tcontent ON (docs.DocType=tcontent.ViewParam) WHERE docs.Laiks>=CurDate()-7 AND tcontent.ViewID=2 ORDER BY docs.Laiks DESC";

                            }
                            else
                            {
                                string WGroups = Session["WhereGroups"].ToString().Replace("AccessU", "reports.AccessU");
                                WGroups = WGroups.Replace("AccessG", "reports.AccessG");
                                qsRep = "SELECT reports.RepID, reports.RepName, reports.RepCaption, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM reports LEFT JOIN tcontent ON (reports.RepGroup=tcontent.ViewParam) WHERE reports.Laiks>=CurDate()-7 AND tcontent.ViewID=3 AND (reports.AccessU LIKE '%;guest;%' OR reports.AccessG LIKE '%;Everyone;%' OR reports.AccessG LIKE '%;Guests;%' OR reports.AccessG LIKE '%;Authenticated users;%' " + WGroups + ") ORDER BY reports.Laiks DESC";
                                WGroups = WGroups.Replace("reports.AccessG", "docs.AccessG");
                                WGroups = WGroups.Replace("reports.AccessU", "docs.AccessU");
                                qsDoc = "SELECT docs.DocID, docs.DocHead, tcontent.TContName, tcontent.ViewParam, tcontent.ViewID FROM docs LEFT JOIN tcontent ON (docs.DocType=tcontent.ViewParam) WHERE docs.Laiks>=CurDate()-7 AND tcontent.ViewID=2 AND (docs.AccessU LIKE '%;guest;%' OR docs.AccessG LIKE '%;Guests;%' OR docs.AccessG LIKE '%;Everyone;%' OR docs.AccessG LIKE '%;Guests;%' OR docs.AccessG LIKE '%;Authenticated users;%' " + WGroups + ") ORDER BY docs.Laiks DESC";

                            }

                        }
                        //jaunās atskaites
                        ds = GFunc.GetData(qsRep, "WebCNS",false);
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                //DataRow row = ds.Tables[0].Rows[0];
                                
                                tblNewRep.Visible = true;
                                foreach (DataRow row in ds.Tables[0].Rows)
                                {
                                    NRow = new TableRow();
                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NHLink.NavigateUrl = "reportlist.aspx?ViewID=" + row["ViewID"].ToString() + "&ViewParam=" + row["ViewParam"].ToString() + "&NText=0";
                                    NHLink.Text = row["TContName"].ToString();
                                    NHLink.Target = "main";
                                    NCell.Controls.Add(NHLink);
                                    NRow.Cells.Add(NCell);

                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NHLink.NavigateUrl = "reportparam.aspx?RepID=" + row["RepID"].ToString() + "&RepName=" + row["RepName"].ToString() + "&RepCaption=" + row["RepCaption"].ToString();
                                    NHLink.Text = row["RepCaption"].ToString();
                                    NHLink.Target = "main";
                                    NCell.Controls.Add(NHLink);
                                    NRow.Cells.Add(NCell);
                                    tblNewRep.Rows.Add(NRow);
                                }
                            }
                        }
                        //jaunie dokumenti
                        ds = GFunc.GetData(qsDoc, "WebCNS",false);
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                //DataRow row = ds.Tables[0].Rows[0];
                                tblNewDocs.Visible = true;
                                foreach (DataRow row in ds.Tables[0].Rows)
                                {
                                    NRow = new TableRow();
                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NHLink.NavigateUrl = "DocList.aspx?ViewID=" + row["ViewID"].ToString() + "&ViewParam=" + row["ViewParam"].ToString() + "&NText=0";
                                    NHLink.Text = row["TContName"].ToString();
                                    NHLink.Target = "main";
                                    NCell.Controls.Add(NHLink);
                                    NRow.Cells.Add(NCell);

                                    NCell = new TableCell();
                                    NHLink = new HyperLink();
                                    NHLink.NavigateUrl = "Doc.aspx?DocID=" + row["DocID"].ToString();
                                    NHLink.Text = row["DocHead"].ToString();
                                    NHLink.Target = "main";
                                    NCell.Controls.Add(NHLink);
                                    NRow.Cells.Add(NCell);
                                    tblNewDocs.Rows.Add(NRow);
                                }
                            }
                        }

                        break;
                    case 1://attēlo kā TreeView apakšlīmeni
                        //for (int i = 0; i < Table1.Rows.Count+1; i++)
                        //{
                        Table1.Visible = true;
                        Table1.Rows.Clear();
                        //}
                        //Response.Write("ViewID=" + VID[0] + ", NodePath=" + NP[0]);
                        string TVWGroups = Session["WhereGroups"].ToString();
                        int CharNum = NP[0].Length + 2;
                        QString = "SELECT * FROM tcontent  WHERE (AccessU LIKE '%;guest;%' OR AccessG LIKE '%;Everyone;%' OR AccessG LIKE '%;Guests;%' " + TVWGroups + ") AND NodePath LIKE '" + NP[0] + "/%' AND LOCATE('/', NodePath," + CharNum + ")=0 ORDER BY NodePath";
                        ds = GFunc.GetData(QString, "WebCNS",false);
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                //DataRow row = ds.Tables[0].Rows[0];

                                foreach (DataRow row in ds.Tables[0].Rows)
                                {
                                    //System.Web.UI.WebControls.hip
                                    //HyperLink H=new HyperLink();
                                    //H.Text = row["TContName"].ToString();
                                    //H.NavigateUrl = "content.aspx?NodeID=" + row["NodePath"].ToString();
                                    //H.Target = "contents";
                                    NewRow = new TableRow();
                                    NewCell = new TableCell();
                                    NewCell.Text = row["TContName"].ToString();
                                    //NewCell.Controls.Add(H);
                                    NewCell.Font.Bold = false;
                                    NewCell.Font.Size = 12;
                                    NewCell.VerticalAlign = VerticalAlign.Middle;
                                    NewCell.HorizontalAlign = HorizontalAlign.Left;
                                    NewRow.Cells.Add(NewCell);
                                    Table1.Rows.Add(NewRow);
                                }
                            }
                        }
                        break;
                    /*case 2:
                        //dokumenti, instrukcijas
                        //Server.Transfer("doclist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0]);
                        Response.Redirect("doclist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0]);
                        break;
                    case 3:
                        //atskaites
                        //Server.Transfer("reportlist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0]);
                        Response.Redirect("reportlist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0]);
                        break;
                    case 4:
                        //Komunikācija
                        //Server.Transfer("comlist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0] + "");
                        Response.Redirect("comlist.aspx?ViewID=" + VID[0] + "&ViewParam=" + VPRM[0] + "&NText=" + NText[0] + "");
                        break;
                     */ 
                    case 2:
                        //tiek atvērts ViewParam norādītais dokuments tiešā veidā 
                        //Server.Transfer(VPRM[0]);
                        Response.Redirect(VPRM[0] + "?" + Request.QueryString);
                        break;
                    default:
                        //Response.Write("<p align='center'><b><font color='#000080' size='5'>SIA &quot;TRIĀLS&quot;</font></b></p><p align='center'><b><font color='#000080' size='5'>INTRANET</font></b></p>");
                        break;

                }
            }
            else
            {
                //Server.Transfer("main.aspx?ViewID=0");
                Response.Redirect("main.aspx?ViewID=0");
            }

        }
    }
}
